package com.ellricky.tv.ui

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.util.ActivityUtil
import com.ellricky.baselib.adapter.CommonGridItemDecoration
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.entity.new.WareDetail
import com.ellricky.baselib.entity.new.WareLearn
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEListCallback
import com.ellricky.baselib.util.NewEntityCallback
import com.ellricky.tv.R
import com.ellricky.tv.adapter.TvCourseAdapter
import com.ellricky.tv.adapter.TvCourseAdapter2
import com.tencent.bugly.crashreport.CrashReport
import kotlinx.android.synthetic.main.tv_activity_course_select.*
import kotlinx.android.synthetic.main.tv_base_layout_header.*
import java.util.*

class TvCourseSelectActivity : BaseTvActivity<Void, BasePresenter<Void>>() {

    private var mStateId: String? = null

    private var mClassTypeId: String? = null
    private var mClassTypeName: String? = null

    private val mCourseAdapter by lazy { TvCourseAdapter2() }

    override fun getContentLayout(): Int {
        return R.layout.tv_activity_course_select
    }

    override fun parseParams() {
        intent.extras?.also {
            mStateId = it.getString("stage_id")
            mClassTypeName = it.getString("name")
            mClassTypeId = it.getString("classTypeId")

            tvCourseName.text = "$mClassTypeName - 课程选择"

            when (mClassTypeName) {
                "美学基础课" -> ivCourseType?.setBackgroundResource(R.drawable.tv_icon_course_1)
                "艺术创造" -> ivCourseType?.setBackgroundResource(R.drawable.tv_icon_course_3)
                "体验课" -> ivCourseType?.setBackgroundResource(R.drawable.tv_icon_course_5)
                "艺术史" -> ivCourseType?.setBackgroundResource(R.drawable.tv_icon_course_2)
            }
        }

    }

    override fun initUIView() {
        ActivityUtil.addActivity(this)

        initRv()
    }

    override fun initOnclick() {
        tvBack.setOnClickListener(this)
        tvLogout.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v?.id) {
            R.id.tvBack -> finish()
            R.id.tvLogout -> logout()
        }
    }

    private fun initRv() {
        mCourseAdapter.also {
            rvCourse.layoutManager = GridLayoutManager(this, 4)
            rvCourse.adapter = it
            rvCourse.addItemDecoration(CommonGridItemDecoration(this, 4, 20, true))

            it.setItemClickListener { position: Int ->
                val data = it.dataSource?.get(position)
                data?.also {
                    fetchCourseDetail(it.ware_id)
                }
            }
        }
    }

    override fun fetchData() {
        val url = EAppUtil.getETvApiHost()

        val paramMap = mutableMapOf<String, String>()
        paramMap["action"] = "teacherCourseWarePrepareList"
        paramMap["course_type_id"] = mClassTypeId ?: ""
        paramMap["course_stage_id"] = mStateId ?: ""
        paramMap["page"] = "1"
        paramMap["page_size"] = "40"
        paramMap["eid"] = LocalCacheUtil.getUid()

        HttpUtils.getInstance().postFrom(url, paramMap, object : NewEListCallback<WareLearn>(this) {
            override fun onSuccess(p0: MutableList<WareLearn>?, p1: String?) {
                p0?.let {
//                    mCourseAdapter.refresh(it)
                    mCourseAdapter.setData(it)
                }
            }
        }, this)
    }

    private fun fetchCourseDetail(id: String) {
        val url = EAppUtil.getETvApiHost()
        val paramMap: MutableMap<String, String> = HashMap()
        paramMap["action"] = "teacherWareDetail"
        paramMap["ware_id"] = id
        paramMap["eid"] = LocalCacheUtil.getUid()

        HttpUtils.getInstance()
            .postFrom(url, paramMap, object : NewEntityCallback<WareDetail>(this) {
                override fun onSuccess(detail: WareDetail, var2: String?) {
                    val file = detail.ware_file
                    EAppUtil.startFileViewerPageOnDetail(this@TvCourseSelectActivity, ".pdf", file)
                }
            })
    }

    override fun onDestroy() {
        super.onDestroy()
        ActivityUtil.removeActivity(this)
        HttpUtils.getInstance().cancelTag(this)
    }
}